<template>
	<zh-form :span="24" ref="rolePageFrom" :model="state.form" :rules="state.rules">
		<zh-form-input prop="roleName" :required="true" label="角色名称" placeholder="请输入角色名称" v-model="state.form.roleName"></zh-form-input>
		<zh-form-select
			prop="roleType"
			label="角色类型"
			placeholder="单选"
			allowClear
			:disabled="true"
			:options="state.dictData.roleType"
			v-model="state.form.roleType"
		></zh-form-select>
		<zh-form-select prop="status" label="状态" placeholder="单选" allowClear :options="state.statusData" v-model="state.form.status"></zh-form-select>
		<zh-form-input prop="remarks" type="textarea" :rows="4" label="备注" v-model="state.form.remarks"></zh-form-input>
		<zh-form-input :style="{ display: 'none' }" prop="roleId" label="角色id" v-model="state.form.roleId"></zh-form-input>
		<zh-form-input :style="{ display: 'none' }" prop="parentId" label="上级角色id" v-model="state.form.parentId"></zh-form-input>
	</zh-form>
</template>

<script lang="ts" setup name="RoleForm">
//------------------接收传入参数(组件)-------------------
const props = defineProps({
	roleInfo: {
		type: Object,
		default: () => {},
		required: false,
	},
	modelValue: Boolean,
});
const emit = defineEmits(['update:modelValue']);

//------------------定义子组件---------------------------
const rolePageFrom = ref<any>();
//------------------内部变量定义-------------------------
const state = reactive({
	dictData: [] as any,
	form: { roleName: undefined, roleType: 'unit', status: undefined, remarks: undefined, roleId: undefined, parentId: undefined },
	statusData: [
		{ dictKey: '1', dictValue: '启用' },
		{ dictKey: '0', dictValue: '禁用' },
	],
	rules: {
		roleName: [{ required: true, message: '请输入角色名' }],
		status: [{ required: true, message: '请输入状态' }],
	},
});

//------------------定义调用方法-------------------------
const saveRole= async () =>{
  //校验表单
	let checkFlag = await rolePageFrom.value.checkForm();
	if (checkFlag) {
    let saveForm=state.form;
		//校验通过
    if (!saveForm.roleId) {
			let res = await $api.common.send.post('role0004',saveForm);
			res.saveType = 'add';
      emit('update:modelValue', false);
			return res;
		} else {
			let res = await $api.common.send.post('role0005',saveForm);
			res.saveType = 'edit';
      emit('update:modelValue', false);
			return res;
		}
  }
}

//------------------初始化------------------------------
// 数据初始化
const initDict=()=>{
  const params = {
    dictFields: 'roleType',
  };
  $api.common.getBatchDict.post(params).then((res) => {
    state.dictData = res.result;
  });
}
state.form = Object.assign(state.form, props.roleInfo);
initDict()
//------------设置给父组件调用的数据/方法(组件)-----------
defineExpose({ form: state.form ,saveRole});
</script>
